define([
    'zepto',
    'underscore',
    'backbone',
    "text!templates/tied/debitCard.html",
    "wTools",
    "area",
    "validate"

], function ($, _, Backbone, template) {
    return Backbone.View.extend({
        $el: $main,
        el: $main0,
        template: _.template(template),
        events: {
            "blur #idCard":"idCardTest",
            "blur #phoneNum":"phoneNumTest",
            "input #idCard":"limitLen",
            "input #phoneNum":"limitLen",
            "tap #btn": "btn"
        },
        initialize: function () {
            $("#head_back").show();
            var that = this;
            if(wData.userNo != '' && wData.userNo != undefined && wData.userNo != null){
                that.render(wData);
            }
            else{
                location.hash = 'bindCard'
            }
        },
        back: function () {
            history.back();
        },
        render: function (data) {
            var html = this.template(data);
            this.$el.html(html);
        },
        btn: function () {
            var bOK = $('#btn').hasClass('disabled' ) || this.isEmpty() || !this.idCardTest() || !this.phoneNumTest();
            if(bOK){
                return;
            };
            var postData = {
                'token':wData.urlData.token,
                'name':$('#username').html().replace(/\s/g, ''),
                'cardNo':$('#bankCode').html().replace(/\s/g, ''),
                'idNo':$('#idCard').val(),
                'phone':$('#phoneNum').val(),
                'cardType':'01'
            };
            wAjaxForm('auth', '', postData, function(result){
                result = JSON.parse(result);

                wData.idNo = $('#idCard').val();
                wData.phone = $('#phoneNum').val();

                if(result.bizStsCd == '1'){
                    location.hash = 'verifyCode';
                }
                else if(result.bizStsCd == '0' && result.sysRtnCd == 'CARD_IS_BIND') {
                    wTips(result.sysRtnDesc);
                }
                else if(result.bizStsCd == '0' && result.sysRtnCd == '') {
                    wTips(result.sysRtnDesc);
                }
                else if(result.status == 'F'){
                    wData.tip = {
                        img: '<img src="images/icon_fail2.png">',
                        text: result.sysRtnDesc
                    }
                    location.hash = 'bindResult';
                }
                else{
                    wTips('请重试');
                }
            });
        },
        idCardTest: function() {
            var data = $('#idCard').val();
            var result = wCheck(data,'idCard');
            $('#idCard').parents('li').find('.tip').html(result.error)
            return result.success
        },
        phoneNumTest: function() {
            var data = $('#phoneNum').val();
            var result =wCheck(data,'mobilePhone');
            $('#phoneNum').parents('li').find('.tip').html(result.error)
            return result.success;
        },
        isEmpty: function() {
            var _el = $('.listInfo').find('input');
            for(var i = 0; i<_el.size();i++){
                if(_el.eq(i).val()==''){
                    this.$isEmpty = true;
                    break;
                }else{
                    this.$isEmpty = false;
                }
            }
            if(!this.$isEmpty){
                $('#btn').removeClass('disabled');
            }
            else{
                $('#btn').addClass('disabled');
            }
            return this.$isEmpty;
        },
        limitLen: function(event) {
            var lengths = 0;
            var id = event.target.id
            if(id == 'idCard'){
                lengths = 18;
            }
            else if(id == 'phoneNum'){
                lengths = 11;
                nonNumeric($('#'+id));
            }
            limitLength($('#'+id),lengths);
            this.isEmpty();
        },

        destroy: function () {
            this.undelegateEvents();
            this.$el.html("");
            delete this;
        }
    });
});